//
//  ZH_MallHomeImageTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/18.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MallHomeImageTableViewCell.h"
@interface ZH_MallHomeImageTableViewCell()<UIScrollViewDelegate, BFSTimerextensionDelegate>
///内容
@property(nonatomic, weak) UIScrollView *scrollView;
///圆点
@property(nonatomic, weak) UIPageControl *page;

/// 定时器
@property (nonatomic, strong) BFSTimerextension  *timerTool;

@property(nonatomic, assign) NSInteger countdown;
/// 当前图片
@property(nonatomic, assign) NSInteger currentItem;
@property (nonatomic,weak)UIImageView *bgView;
@end
@implementation ZH_MallHomeImageTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_MallHomeImageTableViewCell";
    ZH_MallHomeImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_MallHomeImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
    self.isTimingCarousel = YES;
}
///加载子视图约束
-(void)loadSubviewConstraints
{
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        //        make.height.mas_equalTo(HEIGHT_TRANSFORMATION(360));
        make.bottom.equalTo(self.contentView);
    }];
    [self.page mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.scrollView).offset(HEIGHT_TRANSFORMATION(0));
    }];
    
    
    self.bottomLongLineImage.hidden = NO;
}

//更新UI
- (void)updateUI
{
    if(/* DISABLES CODE */ (1)){
        self.currentItem = 0;
        self.countdown = 0;
        NSInteger count = 3;
        
        self.page.numberOfPages = count;
        self.scrollView.contentSize = CGSizeMake(kMainScreenWidth * count, 0);
        //移除所有子视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for(NSInteger i = 0; i < count; i++){
            UIImageView *image = [UIImageView createImageViewWithName:@"home_banner"];
            //            [image sd_setImageWithUrlString:Default_Image defaultImageName:@""];
            [image sd_setImageWithURL:[NSURL URLWithString:Default_Image]];
            image.backgroundColor = kCommonBackgroudColor;
            image.frame = CGRectMake(kMainScreenWidth*i, 0, kMainScreenWidth, self.picHeight);
            image.tag = i;
            [self.scrollView addSubview:image];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageAction:)];
            image.userInteractionEnabled = YES;
            [image addGestureRecognizer:tap];
        }
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
        if(self.isTimingCarousel){
            
            [self.timerTool startTimer:1 repeats:YES];
        }
    }else{
        UIImageView *image = [UIImageView createImageViewWithName:@"home_banner"];
//        [image sd_setImageWithUrlString:Default_Image defaultImageName:@""];
        [image sd_setImageWithURL:[NSURL URLWithString:Default_Image]];
        [self.scrollView addSubview:image];
        image.frame = CGRectMake(0, 0, kMainScreenWidth, self.picHeight);
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
    }
}


///更新UI
- (void)updateUIForImages:(NSMutableArray *)images andDefaultImage:(NSString *)pngImage
{
    if(images.count > 0){
        self.currentItem = 0;
        self.countdown = 0;
        NSInteger count = images.count;
        
        self.page.numberOfPages = count;
        self.scrollView.contentSize = CGSizeMake(kMainScreenWidth * count, 0);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        //移除所有子视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for(NSInteger i = 0; i < count; i++){
            UIImageView *image = [UIImageView createImageViewWithName:@""];
            image.frame = CGRectMake(kMainScreenWidth*i, 0, kMainScreenWidth, self.picHeight);
            image.tag = i;
            //  retina屏幕图片显示问题
            [image setContentScaleFactor:[[UIScreen mainScreen] scale]];
            //  不规则图片显示
            image.contentMode =  UIViewContentModeScaleAspectFill;
            image.autoresizingMask = UIViewAutoresizingFlexibleHeight;
            //  图片大于或小于显示区域
            image.clipsToBounds  = YES;
            
            //            [image sd_setImageWithUrlString:[NSString stringWithFormat:@"%@%@",Img_SERVER,images[i][@"banner_img"]] defaultImageName:pngImage];
//            [image sd_setImageWithUrlString:[NSString stringWithFormat:@"%@%@",Img_SERVER,images[i][@"image"]] defaultImageName:@""];
            [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVER_TEST,images[i][@"banner_img"]]]];
            [self.scrollView addSubview:image];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageAction:)];
            image.userInteractionEnabled = YES;
            [image addGestureRecognizer:tap];
        }
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
        if(self.isTimingCarousel){
            [self.timerTool startTimer:1 repeats:YES];
        }
    }else{
        UIImageView *image = [UIImageView createImageViewWithName:pngImage];
        [self.scrollView addSubview:image];
        image.frame = CGRectMake(0, 0, kMainScreenWidth, self.picHeight);
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
    }
}

///更新UI
- (void)updatesUIForImages:(NSMutableArray *)images andDefaultImage:(NSString *)pngImage
{
    if(images.count > 0){
        self.currentItem = 0;
        self.countdown = 0;
        NSInteger count = images.count;
        
        self.page.numberOfPages = count;
        self.scrollView.contentSize = CGSizeMake(kMainScreenWidth * count, 0);
        //移除所有子视图
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        for(NSInteger i = 0; i < count; i++){
            UIImageView *image = [UIImageView createImageViewWithName:@""];
            image.frame = CGRectMake(kMainScreenWidth*i, 0, kMainScreenWidth, self.picHeight);
            image.tag = i;
            //  retina屏幕图片显示问题
            [image setContentScaleFactor:[[UIScreen mainScreen] scale]];
            //  不规则图片显示
            image.contentMode =  UIViewContentModeScaleToFill;
            image.autoresizingMask = UIViewAutoresizingNone;
            //  图片大于或小于显示区域
            image.clipsToBounds  = YES;
            
//            [image sd_setImageWithUrlString:images[i] defaultImageName:pngImage];
            [image sd_setImageWithURL:[NSURL URLWithString:images[i]]];
            [self.scrollView addSubview:image];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageAction:)];
            image.userInteractionEnabled = YES;
            [image addGestureRecognizer:tap];
        }
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
        if(self.isTimingCarousel){
            [self.timerTool startTimer:0.5 repeats:YES];
        }
    }else{
        UIImageView *image = [UIImageView createImageViewWithName:pngImage];
        [self.scrollView addSubview:image];
        image.frame = CGRectMake(0, 0, kMainScreenWidth, self.picHeight);
        
        if(self.timerTool){
            [self.timerTool stopTimer];
        }
    }
}

///设置圆点显隐
- (void)setPageHidden:(BOOL)hidden
{
    self.page.hidden = hidden;
}

///倒计时回调
- (void)timerfucntionCall
{
    self.countdown++;
    if(self.countdown > 5){
        self.countdown = 0;
        
        self.currentItem++;
        if(self.currentItem >= self.page.numberOfPages){
            self.currentItem = 0;
            [self.scrollView setContentOffset:CGPointMake(self.currentItem * kMainScreenWidth, 0) animated:NO];
        }else{
            [self.scrollView setContentOffset:CGPointMake(self.currentItem * kMainScreenWidth, 0) animated:YES];
        }
        self.page.currentPage = self.currentItem;
    }
}

#pragma mark -- event response

-(void)onImageAction:(UITapGestureRecognizer*)tap
{
    UIView *view = tap.view;
    if(self.onPngClickBlock){
        self.onPngClickBlock(view.tag);
    }
}

#pragma mark - UICollectionViewDelegate

///滑动结束后调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / kMainScreenWidth;
    self.page.currentPage = index;
    self.currentItem = index;
}

#pragma mark -- getter,setter

///内容
-(UIScrollView*)scrollView
{
    if(!_scrollView){
        UIScrollView *scrollView = [UIScrollView createScrollViewWithDelegateTarget:self];
        scrollView.pagingEnabled = YES;
        [self.contentView addSubview:_scrollView = scrollView];
    }
    return _scrollView;
}

///圆点
-(UIPageControl*)page
{
    if(!_page){
        UIPageControl *page = [UIPageControl createPageControl];
        page.numberOfPages = 0;
        page.currentPageIndicatorTintColor = kCommonWhiteBg;
        page.pageIndicatorTintColor = kFontColorBlack;
        [self.contentView addSubview:_page = page];
    }
    return _page;
}

///定时工具
- (BFSTimerextension *)timerTool
{
    if(!_timerTool){
        _timerTool = [[BFSTimerextension alloc] init];
        _timerTool.delegate = self;
    }
    return _timerTool;
}

- (void)setPicHeight:(NSInteger)picHeight
{
    _picHeight = picHeight;
    
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(picHeight);
    }];
}

@end
